<template>
	<view class="tm-signBoard" @touchmove.stop.prevent="stopEvent">
		<!-- @touchmove.stop.prevent="stopEvent" -->
		<!-- #ifdef H5 -->
		
		<canvas
			:style="{
				width: ctx_w + 'px',
				height: ctx_h + 'px'
			}"
			
			@touchstart="touchstart"
			@touchmove.stop.prevent="touchsmove"
			@touchend="touchsend"
			:canvas-id="uid"
			:id="uid"
		></canvas>
		<!-- #endif -->
		<!-- #ifndef H5 -->
		<canvas
			:style="{
				width: ctx_w + 'px',
				height: ctx_h + 'px'
			}"
			@touchstart="touchstart"
			@touchmove="touchsmove"
			@touchend="touchsend"
			canvas-id="tm-signBoard-id"
			id="tm-signBoard-id"
		></canvas>
		<!-- #endif -->
	</view>
</template>

<script>
/**
 * 手写签名板
 * @description 手写签名板,笔峰效果,通过ref执行clear和save功能。
 * @property {Number} line-width = [] 默认：6，线宽度。
 * @property {String} line-color = [] 默认：'#000000'，线颜色
 * @property {Number} width = [] 默认：0， 画布宽，默认使用父组件的宽高。单位upx
 * @property {Number} height = [] 默认：400， 画布高，单位upx
 * @example <tm-signBoard></tm-signBoard>
 */
export default {
	name: 'tm-signBoard',
	props: {
		lineWidth: {
			type: Number,
			default: 6
		},
		lineColor: {
			type: String,
			default: '#000000'
		},
		// 默认使用父组件的宽高。单位upx
		width: {
			type: Number,
			default: 0
		},
		// 单位upx
		height: {
			type: Number,
			default: 400
		}
	},
	data() {
		return {
			ctx: null,
			uid:"f",
			ctx_w: 0,
			ctx_h: 0
		};
	},
	async mounted() {
		let qins = await this.$Querey('.tm-signBoard').catch(e => {});
		// #ifdef H5
		this.uid = this.$tm.guid();
		// #endif
		this.ctx_w = uni.upx2px(this.width) || qins[0].width;
		this.ctx_h = uni.upx2px(this.height) || qins[0].height;
		this.$nextTick(function() {
			// #ifdef H5
			this.ctx = uni.createCanvasContext(this.uid, this);
			// #endif
			// #ifndef H5
			this.ctx = uni.createCanvasContext('tm-signBoard-id', this);
			// #endif
			this.handwriting = new uni.$tm.HandwritingSelf(this.ctx,this.ctx_w,this.ctx_h,this.lineWidth,this.lineColor)
		});
	},
	methods: {
		touchstart(event) {
			event.preventDefault()
			event.stopPropagation()
			if (event.changedTouches.length == 1) {
				var touch = event.changedTouches[0];
				this.handwriting.down(touch.x,touch.y);
			}
		},
		touchsmove(event) {
			event.preventDefault()
			event.stopPropagation()
			if (event.changedTouches.length == 1) {
				var touch = event.changedTouches[0];
				this.handwriting.move(touch.x,touch.y);
			}
		},
		touchsend(event) {
			event.preventDefault()
			event.stopPropagation()
			if (event.changedTouches.length == 1) {
				var touch = event.changedTouches[0];
				this.handwriting.up(touch.x,touch.y);
			}
		},
		clear() {
			if (!this.ctx) return;
			this.ctx.clearRect(0, 0, this.ctx_w, this.ctx_h);
			this.ctx.draw(false);
			this.$nextTick(function() {
				// #ifdef H5
				this.ctx = uni.createCanvasContext(this.uid, this);
				// #endif
				// #ifndef H5
				this.ctx = uni.createCanvasContext('tm-signBoard-id', this);
				// #endif
				this.handwriting = new uni.$tm.HandwritingSelf(this.ctx,this.ctx_w,this.ctx_h,this.lineWidth,this.lineColor)
			});
		},
		save() {
			let t = this;
			return new Promise((su, fa) => {
				if (!this.ctx) {
					fa('ctx未初始化');
					return;
				}
				uni.canvasToTempFilePath(
					{
						x: 0,
						y: 0,
						width: t.ctx_w,
						height: t.ctx_h,
						canvasId: 'tm-signBoard-id',
						success: function(res) {
							// 在H5平台下，tempFilePath 为 base64
							su(res.tempFilePath);
						},
						fail: res => {
							fa(res);
						}
					},
					t
				);
			});
		},
		stopEvent(event) {
			event.preventDefault()
			event.stopPropagation()
		}
	}
};
</script>

<style lang="scss"></style>
